<template>
	<view class="home">
		<uni-search-bar @confirm="searchGoods" v-model="searchKey" />
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
			<swiper-item v-for="(item,index) in bannerGoodsList" :key="index">
				<view class="swiper-item">
					<image :src="item" mode="aspectFill"></image>
				</view>
			</swiper-item>
		</swiper>
		<!-- 底部 -->
		<view class="cateList">
			<view class="item1" v-for="(item,index) in cateOneLst" :key="index">
				<view class="log">
					<image :src="item.icon" mode=""></image>
				</view>
				<view class="name1">{{item.name}}</view>
			</view>
		</view>
		<!-- 精选商品列表 -->
		<view class="showGoods">
			<view class="title">
				精选商品
			</view>
			<view class="list">
				<view class="item" v-for="item,index in selectedLst" :key="indexs" @tap="toDetails(item.id)">
					<view class="img">
						<image :src="item.goods_big_logo" mode=""></image>
					</view>
					<view class="content">
						<view class="name">{{item.name}}</view>
						<view class="info">
							<text class="price">￥{{item.price}}</text>
							<text class="tag">精选</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'

	let bannerGoodsList = ref([])
	let key = ref("")

	onLoad(() => {
		getBanner()
		getCateOneLst()
		getselectedLst()
	})

	function doSearch() {
		uni.showToast({
			// title:"开始搜索",
			title: key.value,
		})
	}

	async function getBanner() {
		let res = await uni.$post("/goodsPromoteSelect", {
			"is_selected": 0,
			"is_promote": 1,
			"page": 1,
			"number": 5
		}).then((res) => {
			console.log(res)
			if (res.code == 200) {
				// 将商品列表中的图片信息提取
				bannerGoodsList.value = res.goodsList.map(item => uni.$base_url + item.goods_big_logo)
			} else {
				uni.showToast({
					icon: "error",
					title: '服务器错误，请稍后重试'
				})
			}
			// console.log(bannerGoodsList)
		}).catch((err) => {
			uni.showToast({
				icon: "error",
				title: '网络请求错误'
			})
		})
	}

	//商品分类列表
	const cateOneLst = ref([])
	async function getCateOneLst() {
		let res = await uni.$post("/categoryListLevel", {
			"level": 0
		})
		//判断返回数据成功
		if (res.code == 200) {
			// 对图片地址添加服务器地址
			cateOneLst.value = res.result.map(item => {
				item.icon = uni.$base_url + item.icon
				return item
			})
			console.log(cateOneLst.value);
		}
	}

	const selectedLst = ref([])
	async function getselectedLst() {
		let res = await uni.$post("/goodsPromoteSelect", {
			"is_selected": 0,
			"is_promote": 1,
			"page": 1,
			"number": 20
		})
		if (res.code == 200) {
			selectedLst.value = res.goodsList.map(item => {
				item.goods_big_logo = uni.$base_url + item.goods_big_logo
				return item
			})
		}
		console.log(selectedLst.value);
	}

	function toDetails(id){
		uni.navigateTo({
			url:`/pages/goodsDetails/goodsDetails?id=${id}`
		})
	}
	
</script>

<style>
	.swiper-item {
		width: 100%;
		height: 300rpx;
	}

	.swiper-item image {
		width: 100%;
		height: 100%;
	}

	/* 商品分类 */
	.cateList {
		margin-top: 40rpx;
		display: flex;
		justify-content: space-around;
	}

	.cateList>.item1>.log {
		width: 60rpx;
		height: 60rpx;
	}

	.cateList>.item1>.log>image {
		width: 100%;
		height: 100%;
	}

	.cateList>.item1>.name1 {
		font-size: 29rpx;
	}

	/*精选商品*/
	/* 	.showGoods {
			padding: 20rpx;
		}
	
		.title {
			font-size: 32rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
			padding-left: 20rpx;
			border-left: 4rpx solid #ff6b81;
		}
	
		.list {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
		}
	
		.item {
			width: 48%;
			background: #fff;
			border-radius: 12rpx;
			margin-bottom: 20rpx;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
			overflow: hidden;
		}
	
		.img {
			width: 100%;
			height: 200rpx;
			overflow: hidden;
		}
	
		.img image {
			width: 100%;
			height: 100%;
			object-fit: cover;
		}
	
		.content {
			padding: 16rpx;
		}
	
		.name {
			font-size: 28rpx;
			color: #333;
			overflow: hidden;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
			min-height: 80rpx;
		}
	
		.info {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 10rpx;
		}
	
		.info text:first-child {
			color: red;
			font-size: 30rpx;
			font-weight: bold;
		}
	
		.tag {
			background: red;
			color: #fff;
			font-size: 24rpx;
			height: 35rpx;
			width: 70rpx;
			text-align: center;
		} */

	/* 商品列表样式 */
	.showGoods {
		padding: 20rpx;
	}

	.title {
		font-size: 32rpx;
		font-weight: bold;
		margin: 20rpx 0;
		padding-left: 20rpx;
		border-left: 4px solid #007AFF;
	}

	.list {
		display: grid;
		grid-template-columns: repeat(auto-fill, minmax(300rpx, 1fr));
		gap: 20rpx;
		padding: 10rpx;
	}

	.item {
		background: #fff;
		border-radius: 15rpx;
		overflow: hidden;
		box-shadow: 0 2px 10px rgba(0,0,0,0.05);
		transition: all 0.3s ease;
	}

	.item:hover {
		transform: translateY(-5rpx);
		box-shadow: 0 5px 15px rgba(0,0,0,0.1);
	}

	.img {
		width: 100%;
		height: 300rpx;
		overflow: hidden;
	}

	.img image {
		width: 100%;
		height: 100%;
		object-fit: cover;
		transition: transform 0.3s ease;
	}

	.img image:hover {
		transform: scale(1.1);
	}

	.content {
		padding: 15rpx;
	}

	.name {
		font-size: 28rpx;
		margin-bottom: 10rpx;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}

	.info {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 10rpx;
	}

	.price {
		color: #ff6b81;
		font-size: 32rpx;
		font-weight: bold;
	}

	.tag {
		background: #ff6b81;
		color: white;
		padding: 4rpx 12rpx;
		border-radius: 20rpx;
		font-size: 24rpx;
	}

	/* 动画效果 */
	@keyframes fadeIn {
		from {
			opacity: 0;
			transform: translateY(20rpx);
		}
		to {
			opacity: 1;
			transform: translateY(0);
		}
	}

	.item {
		animation: fadeIn 0.5s ease-out;
	}
</style>